%% Saving and loading files

%Note the variable present in the workspace as we go along...

clear 

A=4
B=5

%Saves all variable in workspace
save('save1')
clear 

%loads everything saved in 'save1'
load('save1')

%% 

clear 
A=4
B=5
%Save just the variable 'A'
save('save1','A')
clear 
load('save1')

%%
clear 
A=4
B=5
%Save everything
save('save1')
clear 
%Load only 'B'
load('save1','B')

%% 
clear
A=3
B=2
save('save2')
clear
A=10
%This overwrites our value of A!
load('save2','A')

%Load into a structure to avoid overwriting existing variables
L=load('save2')
A=L.A

%%

clear
%Create object

X=MassOnSpring()
save('saveobj')
P=pwd; %current directory
clear X
cd('../') %move up a folder
%this will give an error because class def. is not on path. 
load('saveobj')

%add path and try again
addpath(P)
load('saveobj')

%% Vector and Matrix Operations - Linear Algebra 

%Create two column arrays
A=rand(3,1)
B=rand(3,1)
A.*B %Element wise operation

A*B% Attempted matrix mult. gives error

%create a row and column vector
A=rand(1,3) %row
B=rand(3,1) %column

A*B % This is the same as a dot-product

%% 
 
%Matrix multiplication
A=[1 2; 3 4]
B=[2 3; 4 5]
A*B %This is matrix mult. 
A.*B %This is element-wise operation.  
%Neither gives error.  

%Cross product
X=[1 0 0]
Y=[0 1 0]
Z=cross(X,Y)

%Solving a system of equations
%3x+5*y=5
%2x+10*y=3

%A x = b

A=[3, 5; 2, 10]
b=[5;3]
Ainv=inv(A)

%This should be identity matrix
Ainv*A
eye(2)

%Solve for X
X=Ainv*b

%Check
A*X

%%
%A few more things...

%Rank of matrix.  If rank deficient (rank(A)<size(A,1)), cannot invert
rank(A)

%Rows are not linear independent
B=[1,2; 2,4]
rank(B)

%If determinent is zero, matrix is not invertible
det(B)
det(A)

inv(B)*0

%Get diagonal elements
diag(A)

%%

%Note - careful with spaces when building matricies. 
[3 + 4,2]
[3 +4,2]
[3+4,2]
%Run these and see what you get. 


%% Error Handling

A=rand(4,1)
B=rand(3,1)

%See our function 'DotProd.m'
%Give it various incorrect inputs, and catch them with an error statement

DotProd(A,B)
DotProd(A)

%We haven't protected from every possible error. 










    

























